#include <chrono>
#include <future>
#include <iostream>
#include <thread>

int main() {
  std::future<int> future = std::async(std::launch::async, []() {
    std::this_thread::sleep_for(std::chrono::seconds(3));
    return 8;
  });

  std::cout << "waiting...\n";
  std::future_status status;
  do {
    status = future.wait_for(std::chrono::seconds(1));
    if (status == std::future_status::deferred) {
      std::cout << "deferred\n";
    } else if (status == std::future_status::timeout) {
      std::cout << "timeout\n";
    } else if (status == std::future_status::ready) {
      std::cout << "ready!\n";
    }
  } while (status != std::future_status::ready);

  std::cout << "result is " << future.get() << '\n';
}
